package com.hatcher.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hatcher.entity.User;
import com.hatcher.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Struts2基于注解的Action配置
 * 
 * ParentPackage 继承父包 Namespace命名空间 Results跳转页面 Action访问方法
 */

@SuppressWarnings("serial")
@ParentPackage("struts-default")

@Namespace("/user")

@Results({ @Result(name = "success", location = "/views/main.jsp"),

		@Result(name = "error", location = "/views/error.jsp") })

@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class UserAction extends ActionSupport {
	@Autowired
	private UserService userService;

	@Action(value = "test")
	public String test() {
		return SUCCESS;
	}

	@Action(value = "userView", results = { @Result(name = "success", location = "/views/index.jsp") })
	public String bookView() {
		List<User> list = userService.getUserList();
		ServletActionContext.getRequest().getSession().setAttribute("userList", list);
		return SUCCESS;
	}
}
